package activity;

import java.util.HashMap;
import java.util.Map;

import com.activeandroid.util.Log;
import com.android.volley.VolleyError;
import com.qzhy.female.R;

import Constant.Constance;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import utils.HTTPUtils;
import utils.SetUtils;
import volley.VolleyListener;

public class UpdateUserInfoActivity extends ActionBarActivity implements OnClickListener {

	private TextView mTvAddPhone;
	private TextView mTvPhone;
	private ImageView mImgBack;
	private String phone;
	private TextView mTvNickName;
	private String comfrim_nickname;
	private TextView mTvname;
	private String password;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_update_user_info);
		InitUI();
		InitUserInfo();
	}

	private void InitUserInfo() {
		String userphone = SetUtils.GetUserPhone(getApplicationContext());
		String userNickName = SetUtils.GetUserName(getApplicationContext());
		mTvPhone.setText("绑定号码："+userphone);
		mTvname.setText("修改后的昵称："+userNickName);
	}

	private void InitUI() {
		mTvAddPhone = (TextView) findViewById(R.id.tv_add_phone);
		mTvAddPhone.setOnClickListener(this);
		
		mTvPhone = (TextView) findViewById(R.id.tv_phone);
	    mImgBack = (ImageView) findViewById(R.id.img_back);
	    mImgBack.setOnClickListener(this);
	    
	    mTvNickName = (TextView) findViewById(R.id.tv_nickname);
	    mTvNickName.setOnClickListener(this);
	    
	    mTvname = (TextView) findViewById(R.id.img_circle_cion);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_add_phone:
			startActivityForResult(new Intent(this,AddPhoneActivity.class),Constance.INTENT.UPDATE_PHONE);
			break;
		case R.id.img_back:
			finish();
			break;
		case R.id.tv_nickname:
			UpdateNickName();
			break;
		default:
			break;
		}
	}
	
	private void UpdateNickName() {
		startActivityForResult(new Intent(this,UpdateNickNameActivity.class),Constance.INTENT.UPDATE_NICKNAME);
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
		if(arg2!=null){
			switch (arg1) {
			case Constance.INTENT.COMFIRM_PHONE:
				phone = arg2.getStringExtra("Phone");
				password = arg2.getStringExtra("Password");
				mTvPhone.setText("绑定号码："+phone);
				SavePhone();
				break;
			case Constance.INTENT.COMFIRM_NICKNAME:
				comfrim_nickname = arg2.getStringExtra("NickName");
				mTvname.setText("您已修改昵称:"+comfrim_nickname);
				SavenewNickName();
				break;
			default:
				break;
			}
		}
	}

	private void SavenewNickName() {
		Map<String, String> params=new HashMap<String,String>();
	      params.put("nickname", comfrim_nickname);
	      Toast.makeText(getApplicationContext(), "用户ID:"+SetUtils.GetUserId(getApplicationContext()), 1).show();
	      
		  HTTPUtils.put(getApplicationContext(), Constance.URL.USER+"/"+SetUtils.GetUserId(getApplicationContext()), params, new VolleyListener() {
			@Override
			public void onResponse(String arg0) {
				Log.e("更新信息", arg0.toString());
				Toast.makeText(getApplicationContext(), "用户昵称修改信息"+arg0.toString(), 1).show();
				SetUtils.saveUserName(getApplicationContext(), comfrim_nickname);
			}
			
			@Override
			public void onErrorResponse(VolleyError arg0) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "用户昵称修改失败", 1).show();
			}
		});		
	}

	private void SavePhone() {
      Map<String, String> params=new HashMap<String,String>();
      params.put("phone", phone);
      params.put("password", password);
      Toast.makeText(getApplicationContext(), "用户ID:"+SetUtils.GetUserId(getApplicationContext()), 1).show();
      
	  HTTPUtils.put(getApplicationContext(), Constance.URL.USER+"/"+SetUtils.GetUserId(getApplicationContext()), params, new VolleyListener() {
		@Override
		public void onResponse(String arg0) {
			Log.e("更新信息", arg0.toString());
			Toast.makeText(getApplicationContext(), "用户信息修改信息"+arg0.toString(), 1).show();
			SetUtils.saveUserPhone(getApplicationContext(), phone);
		}
		
		@Override
		public void onErrorResponse(VolleyError arg0) {
			// TODO Auto-generated method stub
			Toast.makeText(getApplicationContext(), "用户信息修改失败", 1).show();
		}
	});		
	}
}
